/*   
 * Copyright (c) 2016-2020 canaanQd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * canaanQd. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with canaanQd.   
 *   
 */ 

package com.lg.utils;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
public class RedisTemplateUtil {

		public static RedisTemplate<String, Object> redisTemplate=SpringContextHolder.getBean("redisTemplate");
		static{
			
		}
	    public static void set(final String key, final Object value, final long expire) {  
	        redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);  
	    }  
	  
	    /** 
	     * 读取缓存 
	     *  
	     * @param key 
	     * @param clazz 
	     * @return 
	     */  
	    @SuppressWarnings("unchecked")  
	    public static <T> T get(final String key, Class<T> clazz) {  
	        return (T) redisTemplate.boundValueOps(key).get();  
	    }  
	      
	    /** 
	     * 读取缓存 
	     * @param key 
	     * @return 
	     */  
	    public static Object getObj(final String key){  
	        return redisTemplate.boundValueOps(key).get();  
	    }  
	  
	    /** 
	     * 删除，根据key精确匹配 
	     *  
	     * @param key 
	     */  
	    public static void del(final String... key) {  
	        redisTemplate.delete(Arrays.asList(key));  
	    }  
	  
	    /** 
	     * 批量删除，根据key模糊匹配 
	     *  
	     * @param pattern 
	     */  
	    public static void delpn(final String... pattern) {  
	        for (String kp : pattern) {  
	            redisTemplate.delete(redisTemplate.keys(kp + "*"));  
	        }  
	    }  
	  
	    /** 
	     * key是否存在 
	     *  
	     * @param key 
	     */  
	    public static boolean exists(final String key) {  
	        return redisTemplate.hasKey(key);  
	    }  
}
